package com.cennavi.minenavidemo.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cennavi.minenavidemo.R;

public class SettingItemView extends RelativeLayout {
    private TextView mSettingItemName;
    private ImageView mSettingItemRightIcon;
    private TextView mSettingItemRightMessage;
    private MyCheckButton mSettingItemRightLocking;
    private OnRightLockingClick mOnRightLockingClick;
    private RelativeLayout mRlItemParent;

    private String mTitleName;

    public interface OnRightLockingClick {
        void onLockingClick();
    }

    public SettingItemView(Context context) {
        this(context, null);
    }

    public SettingItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SettingItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SettingItemView);
        mTitleName = typedArray.getString(R.styleable.SettingItemView_title_name);
        typedArray.recycle();
        init(context);
    }

    public void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.setting_item_view, this);
        mSettingItemName = (TextView) view.findViewById(R.id.item_view_name);
        mSettingItemRightIcon = (ImageView) view.findViewById(R.id.item_view_right_icon);
        mSettingItemRightMessage = (TextView) view.findViewById(R.id.item_view_right_message);
        mSettingItemRightLocking = (MyCheckButton) view.findViewById(R.id.item_view_right_locking);
        mRlItemParent = view.findViewById(R.id.rl_item_parent);

        mSettingItemName.setText(mTitleName);

    }

    /**
     * 设置item右边显示提示信息
     *
     * @param rightMessage 信息
     */
    public void setRightMessage(String rightMessage) {
        mSettingItemRightMessage.setText(rightMessage);
        mSettingItemRightIcon.setVisibility(View.GONE);
        mSettingItemRightLocking.setVisibility(View.GONE);
        mSettingItemRightMessage.setVisibility(View.VISIBLE);
    }

    public CharSequence getRightMessage() {
        return mSettingItemRightMessage.getText();
    }

    /**
     * 设置右边check选择按钮的状态，当调用此方法，默认右边显示check按钮
     *
     * @param isChecked 是否选择
     */
    public void rightLockEnableChecked(boolean isChecked) {
        mSettingItemRightIcon.setVisibility(View.GONE);
        mSettingItemRightMessage.setVisibility(View.GONE);
        mSettingItemRightLocking.setVisibility(View.VISIBLE);
        mSettingItemRightLocking.enableChecked(isChecked);
    }

    public void setOnRightLockingClick(OnRightLockingClick onRightLockingClick) {
        mSettingItemRightLocking.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnRightLockingClick != null) {
                    mOnRightLockingClick.onLockingClick();
                }
            }
        });
        mOnRightLockingClick = onRightLockingClick;
    }

    public boolean rightIsEnableChecked() {
        return mSettingItemRightLocking.isEnableChecked();
    }

}
